package com.vast.metrix.viewer.bustraffic.factory;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.DefaultXYItemRenderer;
import org.jfree.chart.renderer.xy.XYAreaRenderer2;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.util.RelativeDateFormat;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;

public class TimeSeriesChartFactory {

	private TimeSeriesChartFactory(){}
	
	/**
     * Creates a sample chart.
     *
     * @param dataset  the dataset.
     *
     * @return Time Series chart.
     */
	public static JFreeChart createChart(TimeSeriesCollection[] datasetArray, String chartTitle, String xLabel, String[] yLabelArray, double xAxisLengthMilliseconds, boolean isAutoRange, double yAxisLower, double yAxisUpper) {
			
		JFreeChart result = ChartFactory.createTimeSeriesChart(
				chartTitle,      // title
				xLabel,          // x-axis label
				yLabelArray[0],  // y-axis label
				datasetArray[0], // data
                true,            // create legend?
                true,            // generate tooltips?
                false            // generate URLs?
            );
		
		XYPlot plot = (XYPlot) result.getPlot();
		ValueAxis axis = plot.getDomainAxis();
		axis.setFixedAutoRange(xAxisLengthMilliseconds);  // xAxisLength [milliseconds]
		if(isAutoRange) {
			axis.setAutoRange(true);
		} else { 
			axis.setRange(yAxisLower, yAxisUpper);
		}
		
		if(yLabelArray.length > 1) {
			NumberAxis[] rangeAxisArray = new NumberAxis[yLabelArray.length];
			for(int i= 1; i < yLabelArray.length; i++){
				plot.setDataset(i, datasetArray[i]);
				rangeAxisArray[i] = new NumberAxis(yLabelArray[i]);
				rangeAxisArray[i].setAutoRangeIncludesZero(false);
		        plot.setRenderer(i, new DefaultXYItemRenderer());
		        plot.setRangeAxis(i, rangeAxisArray[i]);
				plot.mapDatasetToRangeAxis(i, i);
			}
		}

        return result;
	}
}
